package net.thxjj.adminServer.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.thxjj.adminServer.dao.DeviceDao;
import net.thxjj.adminServer.dao.PushRuleDao;
import net.thxjj.adminServer.entity.*;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.sitermodel.Device;
import net.thxjj.sitermodel.Product;
import net.thxjj.sitermodel.PushRule;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import net.thxjj.adminServer.dao.ProductDao;
import net.thxjj.adminServer.service.IProductService;

@Service
public class ProductService implements IProductService {
	
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private ProductDao productdao;
	@Autowired
	private DeviceDao deviceDao;
	@Autowired
	private PushRuleDao pushRuleDao;
	
	@Value("${upload.root.folder}")
	public String root_fold;
	
	@Value("${img.folder}")
	public String img_fold;
	
	
	@Override
	public List<Product> getProductList(String pid) {
		// TODO Auto-generated method stub
		List<Product> list = productdao.list(pid);
		for (Product product:list){
			List<Device> list1 = deviceDao.getDeviceList("",product.getProdKey(),null,null);
			product.setTotal(list1.size());
		}
		return list;
	}



	@Override
	public HashMap<String, Object> del(String prod_key) {
		// TODO Auto-generated method stub
		try
		{
			productdao.delProduct(prod_key);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResponseModel.getModel(ResultEnum.ERROR, null);
		}

		return ResponseModel.getModel(ResultEnum.SUCCESS, null);
	}

	@Override
	public Product getProduct(String prod_key) {
		return productdao.getProduct(prod_key);
	}

	@Override
	public Long getNumOfDevices(String prod_key) {

		return productdao.getNumOfDevices(prod_key);
	}

	@Override
	public Long getNumOfOnlineDevices(String prod_key) {
		return productdao.getNumOfOnlineDevices(prod_key);
	}

	@Override
	public List<List<Object>> getDeviceRends(String prodKey, long start_time, long end_time) {
		long ads = start_time;

		List<List<Object>> dea = new ArrayList<>();
		List<Device> list = deviceDao.getDeviceListByTimeBefore(prodKey,start_time);
		long count_init = list.size();
		while (ads<=end_time) {

			long end_buf = ads+3600*24*1000;
			List<Device> list1 = deviceDao.getDeviceListByTime(prodKey,ads,end_buf);
			count_init = count_init+list1.size();

			List<Object> listaa = new ArrayList<>();
			listaa.add(ads);
			listaa.add(count_init);
			dea.add(listaa);
			ads+=3600*24*1000;
		}

		return dea;
	}

	@Override
	public HashMap<String, Object> updateSetting(ParameterMap parameterMap) {

		try {
			productdao.editProduct(parameterMap);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseModel.getModel(ResultEnum.ERROR, null);
		}

		return ResponseModel.getModel(ResultEnum.SUCCESS, null);
	}

	@Override
	public List<PushRule> getAllPushRuleByProdKey(String prod_key) {
		return pushRuleDao.getAllPushRuleByProdKey(prod_key);
	}

	@Override
	public HashMap<String, Object> enablePushRule(long enable, long id) {
		try {
			if(enable<0 || enable>1){
				return ResponseModel.getModel(ResultEnum.PARAMETER_VALUE_ERROR, null);
			}

			PushRule pushRule = pushRuleDao.getPushRuleById(id);

			if(pushRule==null){
				return ResponseModel.getModel(ResultEnum.PUSH_RULE_IS_NOT_EXIST, null);
			}

			pushRuleDao.enablePushRule(enable,id);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseModel.getModel(ResultEnum.ERROR, null);
		}
		return ResponseModel.getModel(ResultEnum.SUCCESS, null);
	}


}
